package com.huiyujiaoyu.powertime.model;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;


import com.huiyujiaoyu.base.AppContext;
import com.huiyujiaoyu.util.Constant;

import static android.R.attr.type;
import static com.huiyujiaoyu.base.AppConfig.USERINFO;


/**
 * Created by yujunlong on 2017/4/18.
 */

public class UserInfoBean {
    private static UserInfoBean userInfoBean = null;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;


    private boolean isLogin;//是否已经登录
    private boolean isFirstOpen;//是否第一次打开app



    private String uid;//用户id
    private String token;//用户token
    private String phone;
    private String nickName;
    private String headImg;



    private String gender;



    private String isVip;
    private String signature;
    private String expiry_date;

    private String balance;
    private String address;
    private String score;
    private String is_agent;
    private String wechat;
    private String qq;
    private String blog;


    // 实现了线程安全，又避免了同步带来的性能影响。
    public static UserInfoBean getInstance() {
        if (userInfoBean == null) {
            synchronized (UserInfoBean.class) {
                if (userInfoBean == null) {
                    userInfoBean = new UserInfoBean(AppContext.context());
                }
            }
        }
        return userInfoBean;
    }

    private UserInfoBean(Context context) {
        super();
        init(context);
    }

    public void init(Context context) {
        sp = context.getSharedPreferences(USERINFO, Activity.MODE_PRIVATE);
        editor = sp.edit();
    }
    /**
     * 删除帐号信息
     *
     */
    public void clearUserInfo() {
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.commit();
        //清空信息以后数据就为默认，需要设置是否第一次打开
        setFirstOpen(isFirstOpen);
        getAccess();
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
        editor.putString(Constant.TOKEN, token);
        editor.commit();
    }
    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
        editor.putString(Constant.USERID, uid);
        editor.commit();
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
        editor.putString(Constant.PHONE, phone);
        editor.commit();
    }

    public String getIsVip() {
        return isVip;
    }

    public void setIsVip(String isVip) {
        this.isVip = isVip;
        editor.putString(Constant.IS_VIP, isVip);
        editor.commit();
    }


    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
        editor.putString(Constant.NICKNAME, nickName);
        editor.commit();
    }

    public String getHeadImg() {
        return headImg;
    }

    public void setHeadImg(String headImg) {
        this.headImg = headImg;
        editor.putString(Constant.HEADIMG, headImg);
        editor.commit();
    }

    public boolean isLogin() {
        return isLogin;
    }

    public void setLogin(boolean login) {
        isLogin = login;
        editor.putBoolean(Constant.ISLOGIN, login);
        editor.commit();
    }
    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
        editor.putString(Constant.GENDER, gender);
        editor.commit();
    }


    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
        editor.putString(Constant.SIGNATURE, signature);
        editor.commit();
    }
    public String getExpiry_date() {
        return expiry_date;
    }

    public void setExpiry_date(String expiry_date) {
        this.expiry_date = expiry_date;
        editor.putString(Constant.EXPIRY_DATE, expiry_date);
        editor.commit();
    }
    public boolean isFirstOpen() {
        return isFirstOpen;
    }

    public void setFirstOpen(boolean firstOpen) {
        isFirstOpen = firstOpen;
        editor.putBoolean(Constant.FIRSTOPEN, firstOpen);
        editor.commit();
    }




    //数据共享
    public void setKV(String key, String value) {
        editor.putString(key, value);
        editor.commit();
    }

    public String getValue(String key) {
        return sp.getString(key, "");
    }




    /***
     * 读取配置文件，并赋值
     */
    public void getAccess() {
        isLogin = sp.getBoolean(Constant.ISLOGIN, false);
        uid = sp.getString(Constant.USERID, "0");
        gender = sp.getString(Constant.GENDER,"");
        token = sp.getString(Constant.TOKEN, "");
        phone = sp.getString(Constant.PHONE, "");
        nickName = sp.getString(Constant.NICKNAME,"");
       headImg  = sp.getString(Constant.HEADIMG,"");
        isVip  = sp.getString(Constant.IS_VIP,"");
        signature  = sp.getString(Constant.SIGNATURE,"");
        expiry_date  = sp.getString(Constant.EXPIRY_DATE,"");
        balance  = sp.getString(Constant.BALANCE,"");
        address  = sp.getString(Constant.ADDRESS,"");
        score  = sp.getString(Constant.SCORE,"");
        is_agent  = sp.getString(Constant.IS_AGENT,"");
        wechat  = sp.getString(Constant.WECHAT,"");
        qq  = sp.getString(Constant.QQ,"");
        blog  = sp.getString(Constant.BLOG,"");
        isFirstOpen  = sp.getBoolean(Constant.FIRSTOPEN,true);
    }

    /***
     * 写入登录信息配置文件
     */
    public void setAccess(LoginBean bean) {
        editor.putString(Constant.USERID, bean.user_id);
        editor.putString(Constant.PHONE, bean.phone);
        editor.putString(Constant.TOKEN, bean.token);
        editor.putString(Constant.NICKNAME, bean.name);
        editor.putString(Constant.HEADIMG, bean.photo);
        editor.putString(Constant.GENDER, bean.photo);
        editor.putString(Constant.IS_VIP, bean.is_vip);
        editor.putString(Constant.SIGNATURE, bean.signature);
        editor.putString(Constant.EXPIRY_DATE, bean.expiry_date);
        editor.putString(Constant.BALANCE, bean.balance);
        editor.putString(Constant.ADDRESS, bean.address);
        editor.putString(Constant.SCORE, bean.score);
        editor.putString(Constant.IS_AGENT, bean.is_agent);
        editor.putString(Constant.WECHAT, bean.wechat);
        editor.putString(Constant.QQ, bean.qq);
        editor.putString(Constant.BLOG, bean.blog);
        editor.commit();
        getAccess();

    }


    public String getBalance() {
        return balance;
    }

    public void setBalance(String balance) {
        this.balance = balance;
        editor.putString(Constant.BALANCE, balance);
        editor.commit();
    }
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
        editor.putString(Constant.ADDRESS, address);
        editor.commit();
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
        editor.putString(Constant.SCORE, score);
        editor.commit();
    }

    public String getIs_agent() {
        return is_agent;
    }

    public void setIs_agent(String is_agent) {
        this.is_agent = is_agent;
        editor.putString(Constant.IS_AGENT, is_agent);
        editor.commit();
    }

    public String getWechat() {
        return wechat;
    }

    public void setWechat(String wechat) {
        this.wechat = wechat;
        editor.putString(Constant.WECHAT, wechat);
        editor.commit();
    }

    public String getQq() {
        return qq;
    }

    public void setQq(String qq) {
        this.qq = qq;
        editor.putString(Constant.QQ, qq);
        editor.commit();
    }

    public String getBlog() {
        return blog;
    }

    public void setBlog(String blog) {
        this.blog = blog;
        editor.putString(Constant.BLOG, blog);
        editor.commit();
    }
}
